package com.baosight.sczx.zm.cs.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.Mine;
import com.baosight.sczx.zm.cs.domain.ZMCSWorkSchedule;

/**
 * 
 * <p>Title: ServiceZMCS70.java</p>
 * <p>Description: 调度班次设置</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author bobswang
 * @version 1.0
 * <pre>Histroy:
 *          2011-2-18 下午01:16:20  bobswang  Create
 *</pre>
 */
public class ServiceZMCS70 extends ServiceEPBase 
{
	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化页面信息</p>
	 * <p>return value: EiInfo</p>
	 * <p>History:
	 *    2011-2-18下午01:21:00 bobswang
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo)
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "100");//设置每页默认显示记录数
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: 查询班次信息</p>
	 * <p>return value: EiInfo</p>
	 * <p>History:
	 *    2011-2-18下午01:21:32 bobswang
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo)
	{
		EiInfo outInfo = super.query(inInfo, "ZMCSWorkSchedule.query", new ZMCSWorkSchedule());
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.scheduleclassify", new String[] { "三班制", "四班制" }, new String[] { "三班制", "四班制" }, false));// 班次分类
		outInfo.addBlock(Mine.getMineBlock("cmes.cs.mine"));
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: 修改班次信息</p>
	 * <p>return value: EiInfo</p>
	 * <p>History:
	 *    2011-2-18下午01:21:53 bobswang
	 * </p>
	 *
	 */
	public EiInfo update(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCSWorkSchedule _dom = new ZMCSWorkSchedule();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			String isStart = _dom.getFdIsStart().toString();
			if("1".equals(isStart))
			{
				Map<String, String> inMap = new HashMap<String, String>();
				inMap.put("fdScheduleClassify", _dom.getFdScheduleClassify().toString());
				dao.update("ZMCSWorkSchedule.updateStartFlag", inMap);
			}
			this.setFdIsThrough(_dom); 
			this.dao.update("ZMCSWorkSchedule.update", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
		
		 
	}
	
	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: 新增班次信息</p>
	 * <p>return value: EiInfo</p>
	 * <p>History:
	 *    2011-2-18下午01:22:41 bobswang
	 * </p>
	 *
	 */
	public EiInfo insert(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCSWorkSchedule _dom = new ZMCSWorkSchedule();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			String isStart = _dom.getFdIsStart().toString();
			if("1".equals(isStart))
			{
				Map<String, String> inMap = new HashMap<String, String>();
				inMap.put("fdScheduleClassify", _dom.getFdScheduleClassify().toString());
				dao.update("ZMCSWorkSchedule.updateStartFlag", inMap);
			}
			this.setFdIsThrough(_dom); 
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CI_WORKSCHEDULE"));
			_dom.setFdRemark("");
			this.dao.insert("ZMCSWorkSchedule.insert", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	/**
	 * 
	 * <p>Function: delete</p>
	 * <p>Description: 删除调度班次</p>
	 * <p>return value: EiInfo</p>
	 * <p>History:
	 *    2011-2-18下午05:29:08 bobswang
	 * </p>
	 *
	 */
	public EiInfo delete(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCSWorkSchedule _dom = new ZMCSWorkSchedule();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i)); 
			this.dao.delete("ZMCSWorkSchedule.delete", _dom);	
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}
	
	/**
	 * 
	 * <p>Function: validate</p>
	 * <p>Description: 验证是否已经有班次存在</p>
	 * <p>return value:boolean</p>
	 * <p>History:
	 *    2011-2-18下午06:30:56 bobswang
	 * </p>
	 *
	 */ 
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo)
	{
		HashMap<String, String> params = new HashMap<String, String>();
		ZMCSWorkSchedule _dom = new ZMCSWorkSchedule();
		String oper=(String)inInfo.get("efCurButtonEname");
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdMiningId", _dom.getFdMiningId());
			params.put("fdScheduleNum", _dom.getFdScheduleNum());
			params.put("fdScheduleClassify", _dom.getFdScheduleClassify());
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdKeyId", _dom.getFdKeyId());
			}
			List result = dao.query("ZMCSWorkSchedule.validate", params);
			if (result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				ZMCSWorkSchedule _temp = (ZMCSWorkSchedule) result.get(0);
				StringBuffer msg = new StringBuffer("提交失败，");
				if (_temp.getFdScheduleNum()!= null && _temp.getFdScheduleNum().equals(_dom.getFdScheduleNum().toString())) {
					msg.append(" 班次 ");
				}
				if (_temp.getFdScheduleClassify() != null && _temp.getFdScheduleClassify().equals(_dom.getFdScheduleClassify().toString())) {
					msg.append(" 班次分类 ");
				}
	/*			if (_temp.getFdAbbreviation() != null
						&& _temp.getFdAbbreviation().equals(fdAbbreviation)) {
					msg.append(" 简称 ");
				}*/
				msg.append("重复！");
				inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
	/**
	 * 设置fdIsThrough字段值
	 * <p>Function: setFdIsThrough</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-25上午06:49:53 严洪浩
	 * </p>
	 *
	 */
	private void setFdIsThrough(ZMCSWorkSchedule _dom) {
		String fdStartTime = _dom.getFdStartTime().toString();//开始时间
		String fdEndTime =  _dom.getFdEndTime().toString();//结束时间
		
		if(null != fdStartTime && !"".equals(fdStartTime)) {
			fdStartTime = fdStartTime.replace(":", "");
		}
		
		if(null != fdEndTime && !"".equals(fdEndTime)) {
			fdEndTime = fdEndTime.replace(":", "");
		}
		
		int startTime = Integer.parseInt(fdStartTime);
		int endTime = Integer.parseInt(fdEndTime);
		
		if(endTime < startTime) {
			_dom.setFdIsThrough((long) 1);
		}
		else {
			_dom.setFdIsThrough((long) 0);
		}
	} 
}
